package T21_30;

public class T29 {
    public static class ListNode {
        int val;
        ListNode next;
        ListNode() {}
        ListNode(int val) { this.val = val; }
        ListNode(int val, ListNode next) { this.val = val; this.next = next; }
    }
    public ListNode removeNthFromEnd(ListNode head, int n) {
            /*思路：1 快慢指针，让fast先走n步，然后fast和slow一起移动
             * 2 当fast.next为空时，此时slow指向要删除结点的前一个结点*/
            ListNode res = new ListNode(0);
            ListNode fast=res;
            ListNode slow=res;
            res.next=head;
            while(n>0){
                fast=fast.next;
                n--;
            }
            while (fast.next!=null){
                fast=fast.next;
                slow=slow.next;
            }
            slow.next=slow.next.next;
            return res.next;
    }
}
